package com.atguigu.spzx.manager.service.product.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.product.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mahone
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public PageInfo<Product> findProductListByPage(Integer pageNum, Integer pageSize,ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.selectProductListByPage(productDto);
        return new PageInfo<>(productList);
    }

    @Override
    public void insertProduct(Product product) {

        // 商品表的Dao

        // 设置上架状态为0
        product.setStatus(0);
        // 设置审核状态为0
        product.setAuditStatus(0);
        productMapper.insertProduct(product);

        // 保存sku表数据
        int  i = 0;
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            i++;
            productSku.setProductId(product.getId());
            productSku.setSkuCode(product.getId() + "_" + i);
            productSku.setSkuName(product.getName() + " " + productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSkuMapper.insertProductSku(productSku);
        }

         //  插入detail表数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailMapper.insertProductDetail(productDetails);
    }

    @Override
    public Product updateProductById(Long id) {
        // 查询product表
        Product productInfo = productMapper.selectProductById(id);

        // 封装sku表
        List<ProductSku> productSkuList = productSkuMapper.selectProductSkuById(id);
        productInfo.setProductSkuList(productSkuList);

        // 封装海报数据
        ProductDetails productDetails = productDetailMapper.selectProductDetailById(id);
            productInfo.setDetailsImageUrls(productDetails.getImageUrls());

        return productInfo;
    }

    @Override
    public void updateProduct(Product product) {
        // 修改product
        productMapper.upDateById(product);
        // 根据productId删除sku
        productSkuMapper.deleteByProductId(product.getId());
        // 重新添加sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSkuMapper.insertProductSku(productSku);
        }
        // 修改details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailMapper.upDateByProductId(productDetails);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if(auditStatus == 1){
            product.setAuditStatus(auditStatus);
            product.setAuditMessage("审批通过");
        }else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.upDateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.upDateById(product);
    }
}
